**********************************
*	Title: regloop10.do
*	Date: 16 December 2007; updated 20 Feb; modified 13 March; modified 22 Nov 2008; modified April 2009;
*	Author: Zoe McLaren, Jim Levinsohn
*	Description: 
*		1. Produces results for unconditional effect, propensity score weighting, and control function
*		2. Prints results in matrices table1, N, ctrltable2 and N2
**********************************

version 8
tab african, mi

** Create subgroups: Estimate pscore separately for each subgroup
gen subgroup1=1 if nea~= 1
gen subgroup2=1 if nea~= 1 & female==0
gen subgroup3=1 if nea~= 1 & female==1
gen subgroup4=1 if nea~= 1 & age>=25 & (matric==1 | tertiary==1)
gen subgroup5=1 if nea~= 1 & age>=25 & (matric==0 & tertiary==0)
gen subgroup6=1 if nea~= 1 & urban==0
gen subgroup7=1 if nea~= 1 & urban==1


* Table A1 - Narrow definition of unemployment (Must be searching to be unemployed)
replace nea=1 if employment==4
replace unemployed=0 if employment==4
label var unemployed "Narrow definition (employment==4 is discouraged/NEA)"


/*
* Table A2 - Includes NEA
cap drop subgroup*
gen subgroup1=1
gen subgroup2=1 if female==0
gen subgroup3=1 if female==1
gen subgroup4=1 if age>=25 & (matric==1 | tertiary==1)
gen subgroup5=1 if age>=25 & (matric==0 & tertiary==0)
gen subgroup6=1 if urban==0
gen subgroup7=1 if urban==1
*/

*********************
#delimit;
global instruments `"program1 program2 program3 program4 program5 program6 program7 program8 
		trans_needle trans_cig trans_oral trans_toilet 
		 trans_birth trans_cup trans_anal trans_air trans_blood trans_touch AIDScure HIVtoAIDS cure_vir HIVsin 
		  stigmaA stigmaB stigmaC stigmaD stigmaE stigmaF stigmaG posneg responsible responsiblegovt selfrisk1 paststd
		binge1 binge2 binge3 binge4 binge5 remember1 remember2 remember3 remember4 remember5 injury "';
#delimit cr

lab define correct 1 "correct" 2 "incorrect/dk/missing"
foreach var of varlist trans_needle trans_oral trans_birth trans_anal trans_blood HIVtoAIDS stigmaA stigmaB stigmaC posneg{
	recode `var' (2 3 . = 0) /* 1 is correct */
	lab val `var' correct
}
foreach var of varlist trans_cig trans_toilet trans_cup trans_air trans_touch AIDScure cure_vir HIVsin stigmaD stigmaE stigmaF stigmaG {
	recode `var' (1 3 . = 0) (2 = 1)  /* 2 is correct */
	lab val `var' correct
}
gen paststd=(everdischarge==1 | everulcer==1)
gen paststd_d=(everdischarge==. & everulcer==.)
foreach var of global instruments {
	assert (`var'==0 | `var'==1) 
}

*Results loop begins here
forvalues j = 1/7 {
*Initialize results matrices
matrix input results = (0, 0, 0, 0)
matrix input N = (0, 0, 0, 0)

	foreach x in list positive naive naiven ols olsn olstemp reweight reweightn control controln {
		matrix input `x' = ()
	}

	cap drop pscore
	cap drop pscore2
	cap drop pscore3
	cap drop pscoreweight
	
/*  Here is the pscore regression */
	logit positive $special [pw=weight] if subgroup`j'==1
	estimates store pscore`j'
	local R2=e(r2_p)
	estadd scalar pR2=`R2'
	*Output for Table 4 created here	
	cap noisily estout pscore1 using "$output/table4.txt", replace type style(tex) varlabels(_cons \_cons ) cells(b(star fmt(3)) se(par fmt(3))) dropped starlevels(* 0.10 ** 0.05 *** 0.01) stats( pR2 N , fmt(2 %12.0fc))
	predict pscore if e(sample), p
	gen pscoreweight = pscore/(1-pscore)
	replace pscoreweight=1 if positive==1
	replace pscoreweight = pscoreweight*weight
	gen pscore2 = pscore^2
	gen pscore3 = pscore^3
	global pscorefunc = "pscore pscore2 pscore3"

*balancing test
	foreach var of varlist $allvars {
		qui reg `var' positive [pw=pscoreweight] if subgroup`j'==1
		if ((_b[positive]/_se[positive])>=1.95 | (_b[positive]/_se[positive])<=-1.95) & _b[positive]~=0 { 
			di "subgroup `j' is unbalanced on `var'"
		}
	}



***********************************************

*naive
		logit unemployed positive [pweight=weight] if subgroup`j'==1
		mfx
		matrix naive = naive \ e(Xmfx_dydx)
		matrix naive = naive \ e(Xmfx_se_dydx)
		matrix naiven = naiven \e(N)
		matrix naiven = naiven \ 10001  
	
*ols
		logit unemployed positive $special [pweight=weight] if subgroup`j'==1
		mfx
		matrix olstemp = olstemp \ e(Xmfx_dydx)
		matrix olstemp = olstemp \ e(Xmfx_se_dydx)
		matrix ols = ols \ olstemp[1,1]
		matrix ols = ols \ olstemp[2,1]
		matrix olsn = olsn \e(N)
		matrix olsn = olsn \ 10001 

*reweighted
		logit unemployed positive [pweight=pscoreweight] if subgroup`j'==1
		mfx
		matrix reweight = reweight \ e(Xmfx_dydx)
		matrix reweight = reweight \ e(Xmfx_se_dydx)
		matrix reweightn = reweightn \e(N)
		matrix reweightn = reweightn \ 10001  

*control function

		cap drop yhat0
		cap drop cftot
		cap drop yhat1
		logit unemployed $special $pscorefunc [pweight=weight] if positive==0 & subgroup`j'==1
		matrix controln = controln \e(N)
 		predict yhat0 if positive==1 & subgroup`j'==1
		logit unemployed $special $pscorefunc [pweight=weight] if positive==1 & subgroup`j'==1
 		predict yhat1 if positive==1 & subgroup`j'==1
		gen cftot = yhat1-yhat0 if positive==1 & subgroup`j'==1
		sum cftot
		matrix controln = controln \ r(N)
		matrix control = control \ r(mean)
		matrix control = control \ r(Var)  
***End of new section.



	matrix results = results \ naive, ols, reweight, control[1..2,1]
	matrix N = N \ naiven, olsn, reweightn, controln

matlist results, format(%7.3g)
matlist N


}

save "$output/regloop10end.dta", replace
*exit

*Code to obtain bootstrapped SEs for RW and CF
matrix input total = (0,0)
forvalues j = 1/7 {
	use "$output/regloop10end.dta", clear
	do "$syntax/hivlabor_bootstrap3.do" `j'
		matrix total = total \ bsse`j'
	matlist total, format(%7.3g)
}

matlist total, format(%7.3g)


exit
